#include "dialog_selectworkgroup.h"
#include "ui_dialog_selectworkgroup.h"
#include <QDebug>
#include <QMessageBox>
#include <QString>

Dialog_selectWorkGroup::Dialog_selectWorkGroup(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog_selectWorkGroup)
{
    ui->setupUi(this);
    initData();
}

Dialog_selectWorkGroup::~Dialog_selectWorkGroup()
{
    delete ui;
    delete settings;

}

void Dialog_selectWorkGroup::on_btn_ok_clicked()
{
//    if(ui->spinBox_H->value() >= 700){
//        if (QMessageBox::Yes != QMessageBox::question(this, "检查产品尺寸", QString("请确认该产品的尺寸为: %1\n属于大工件，请认真检查产品尺寸信息和配方！").arg(ui->lineEdit_size->text()), QMessageBox::Yes | QMessageBox::No, QMessageBox::No)) {
//            return;
//        }
//    }

    QString devSize = ui->comboBox_devCurrentSize->currentText();
    QString groupSize = ui->comboBox_groupCurrentSize->currentText();

    if(devSize != groupSize)
    {
        if (QMessageBox::Yes != QMessageBox::question(this, "报错", QString("当前类型不匹配，当前：%1,工作组：%2").arg(devSize).arg(groupSize), QMessageBox::Yes | QMessageBox::No, QMessageBox::No)) {

        }
         return;
    } else {

    }


    settings->beginGroup("current");
    settings->setValue("group", ui->comboBox_name->currentText());

    emit currentGroupName(ui->comboBox_name->currentText());
    emit currentGroupSize(ui->lineEdit_size->text());

    accept();
}


void Dialog_selectWorkGroup::on_btn_cancel_clicked()
{
    reject();
}

void Dialog_selectWorkGroup::initData()
{
    QRegExp regExp("^\\d+\\*\\d+\\*\\d+$"); // 正则表达式，匹配形如"L*H*G"的格式
    QValidator *validator = new QRegExpValidator(regExp, this);
    ui->lineEdit_size->setValidator(validator);


    m_iniManager = new iniManager("config/workGroupConfig.ini");

//    // 创建 QSettings 对象并加载 INI 文件
//    settings = new QSettings("config/workGroupConfig.ini", QSettings::IniFormat);

    // 获取所有的组
    QStringList groups = m_iniManager->allGroups();

    if (groups.isEmpty()) {
        qDebug() << "No groups found in the INI file.";
    } else {
        // 连接槽
        connect(ui->lineEdit_size, &QLineEdit::textChanged, [=](QString str){

            if(str.isEmpty()){

                return;
            }

            QStringList str_list = str.split("*");

            if(str_list.size() != 3)
            {
                return;
            }

            m_currL = str_list.at(0).toInt();
            m_currW = str_list.at(1).toInt();
            m_currH = str_list.at(2).toInt();
        });

        // 初始化当前组索引
        int currentGroupIndex = 0;
        for(int i=0;i<groups.count();i++,currentGroupIndex++){
            // 显示当前组信息
            QString currentGroup = groups[currentGroupIndex];
            qDebug() << "Current group:" << currentGroup;

            // 显示当前组的所有键值对
//            settings->beginGroup(currentGroup);
            QStringList keys = m_iniManager->allKeys(currentGroup);
            foreach(const QString& key, keys) {
                // qDebug() << key << "=" << settings->value(key).toString();
                if(key=="name")
                    ui->comboBox_name->addItem(m_iniManager->readValue(currentGroup, key).toString());
//                if(currentGroupIndex==0)
//                    ui->lineEdit_size->setText(m_iniManager->readValue(currentGroup, "size").toString());
            }
//            settings->endGroup();
        }

//        settings->beginGroup("current");

        ui->comboBox_name->setCurrentText(m_iniManager->readValue("current", "group").toString());
//        settings->endGroup();
        on_comboBox_name_currentIndexChanged(ui->comboBox_name->currentText());
    }
}

void Dialog_selectWorkGroup::on_comboBox_name_currentIndexChanged(const QString &arg1)
{
    QStringList arg1List = arg1.split("_");
    qDebug() << arg1List;

    if(arg1List.size() < 4)
    {
        qDebug() << "工作组编号，拆分错误";
        return;
    }

//    settings->beginGroup(arg1List.at(0));

    ui->lineEdit_size->setText(m_iniManager->readValue(arg1List.at(0), "size").toString());
//    settings->endGroup();



    if(m_currW <= 220)
    {
        ui->comboBox_groupCurrentSize->setCurrentText("窄");
    } else {
        ui->comboBox_groupCurrentSize->setCurrentText("宽");
    }

    //    emit currentGroupName(ui->comboBox_name->currentText());
}

void Dialog_selectWorkGroup::do_setCurrentGroupSize(const QString &currentGroupSize)
{
    ui->comboBox_devCurrentSize->setCurrentText(currentGroupSize);
}

